package com.ruoyi.religion.sdk;


import com.ruoyi.religion.lib.NetDEVSDKLib;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import static com.ruoyi.religion.lib.NetDEVSDKLib.NETDEV_LOGIN_PROTO_E.NETDEV_LOGIN_PROTO_ONVIF;
import static com.sun.jna.Pointer.NULL;

public class Test {

    static NetDEVSDKLib netdevsdk = NetDEVSDKLib.NETDEVSDK_INSTANCE;
    static Pointer lpUserID = null;
    static Log log = LogFactory.getLog(NetDemo.class);

    public static void main(String[] args) {

        netdevsdk.NETDEV_Init();

        //设置连接时间
        NetDEVSDKLib.NETDEV_REV_TIMEOUT_S stRevTimeout = new NetDEVSDKLib.NETDEV_REV_TIMEOUT_S();
        stRevTimeout.dwRevTimeOut = 5;
        stRevTimeout.dwFileReportTimeOut = 30;
        netdevsdk.NETDEV_SetRevTimeOut(stRevTimeout);


        //登录参数，包括设备地址、登录用户、密码等
        NetDEVSDKLib.NETDEV_DEVICE_LOGIN_INFO_S stDevLoginInfo = new NetDEVSDKLib.NETDEV_DEVICE_LOGIN_INFO_S();

        String strUserName = "admin";
        String strPassword = "password";
        String strIp = "103.109.127.254";
        String strPort = "80";

        System.arraycopy(strUserName.getBytes(), 0, stDevLoginInfo.szUserName, 0, strUserName.getBytes().length);
        System.arraycopy(strPassword.getBytes(), 0, stDevLoginInfo.szPassword, 0, strPassword.getBytes().length);
        System.arraycopy(strIp.getBytes(), 0, stDevLoginInfo.szIPAddr, 0, strIp.getBytes().length);
        stDevLoginInfo.dwPort = Integer.parseInt(strPort);
        stDevLoginInfo.dwDeviceType = 0;
        stDevLoginInfo.dwLoginProto = NETDEV_LOGIN_PROTO_ONVIF;

        //输出参数，仅私有协议登录有效
        NetDEVSDKLib.NETDEV_SELOG_INFO_S stSELogInfo = new NetDEVSDKLib.NETDEV_SELOG_INFO_S();
        lpUserID = netdevsdk.NETDEV_Login_V30(stDevLoginInfo, stSELogInfo);

        if (NULL == lpUserID) {
            log.info("登录失败" );
            netdevsdk.NETDEV_Logout(lpUserID);
            log.info("已注销" );
        }else{
            log.info("登录成功" );
        }

        //获取通道状态
        int nMaxChlCount = 256;
        IntByReference dwChlCount = new IntByReference(nMaxChlCount);
        NetDEVSDKLib.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S[] stVideoChlList = (NetDEVSDKLib.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S[]) new NetDEVSDKLib.NETDEV_VIDEO_CHL_DETAIL_INFO_EX_S().toArray(nMaxChlCount);
        boolean bRet = netdevsdk.NETDEV_QueryVideoChlDetailListEx(lpUserID, dwChlCount, stVideoChlList);

        if (bRet) {
            for (int i = 0; i < dwChlCount.getValue(); i++) {

                if (Common.DevideStatusToString(stVideoChlList[i].enStatus).equals("online")){

                    int dwChannelID = stVideoChlList[i].dwChannelID;
                    boolean b = netdevsdk.NETDEV_CaptureNoPreview(lpUserID,
                            dwChannelID,
                            NetDEVSDKLib.NETDEV_LIVE_STREAM_INDEX_E.NETDEV_LIVE_STREAM_INDEX_MAIN,
                            "D:/tmp/" + dwChannelID,
                            NetDEVSDKLib.NETDEV_PICTURE_FORMAT_E.NETDEV_PICTURE_JPG
                    );


                }
            }
        }

    }

}
